package org.imaginedays.androidall.utils;

/**
 * 文件大小计算
 * 
 * @author imaginedays
 * 
 */
public class FileSizeCalculateUtil {

	public static String formatCount(long count, boolean rate) {
		// Converts the supplied argument into a string.
		// 'rate' indicates whether is a total bytes, or bits per sec.
		// Under 2Mb, returns "xxx.xKb"
		// Over 2Mb, returns "xxx.xxMb"
		if (count < 1e6 * 2)
			return ((float) ((int) (count * 10 / 1024)) / 10 + (rate ? "kbps"
					: "kB"));
		return ((float) ((int) (count * 100 / 1024 / 1024)) / 100 + (rate ? "mbps"
				: "MB"));
	}

	public static String formatCountMB(long count) {
		// Converts the supplied argument into a string.
		// returns "xxx.xxMb"

		if (count < 1024) // Bytes
			return (((long) (count)) + "B");
		else if ((count >= 1024) & (count < 1048576)) // Kilobytes (1024*1024)
			return ((float) ((long) (count * 100 / 1024)) / 100 + "KB");
		else if ((count >= 1048576) & (count < 1073741824)) // Megabytes
															// (1024*1024*1024)
			return ((float) ((long) (count * 100 / 1024 / 1024)) / 100 + "MB");
		else
			// if ((count >= 1073741824) & (count <
			// (long)(1024*1024*1024*1024))) // Gigabytes (1024*1024*1024*1024)
			return ((float) ((long) (count * 100 / 1024 / 1024 / 1024)) / 100 + "GB");
		// FAILS TO WORK, Numbers too large
		// else if (count <= (1024*1024*1024*1024*1024)) // Terabytes
		// (1024*1024*1024*1024*1024)
		// return ((float)((long)(count*100/1024/1024/1024/1024))/100 + "TB");
		// else if (count > (1024*1024*1024*1024*1024)) // Petabytes
		// (1024*1024*1024*1024*1024*1024)
		// return ((float)((long)(count*100/1024/1024/1024/1024/1024))/100 +
		// "PB");

	}

}
